package cn.woowc.through.thread;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @version V1.0.0
 * @ClassName: {@link ConsoleStreamRunnable}
 * @Description: 数据输出流
 * @author: Lanzhou
 * @date: 2021/4/30 16:37
 * @Copyright:2020 All rights reserved.
 */
@Slf4j
public class ConsoleStreamRunnable implements Runnable {

    private InputStream inputStream;

    private String level;

    public ConsoleStreamRunnable(InputStream inputStream, String level) {
        this.inputStream = inputStream;
        this.level = level;
    }

    @Override
    public void run() {
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder buf = new StringBuilder();
        try {
            String line = null;
            while ((line = br.readLine()) != null) {
                buf.append(line);
            }
            log.debug("[ {} ]输出结果为：[ {} ]", this.level, buf);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }
}
